local skynet = require "skynet"
local utils = require "utils"

local function formatmsg()
	local msg = ""
	local info = debug.getinfo(3)
	if info then
		local index = string.find(info.short_src, '/',5)  -- 这里以"/"为例  
		index = index or 3
		local src = string.sub(info.short_src,index + 1)
		msg = string.format("[%s:%d]",src, info.currentline)
	end
	return msg
end

function log_print(...)
	local info = debug.getinfo(2)
	local index = string.find(info.short_src, '/',5)  -- 这里以"/"为例  
	-- print(info.short_src,index)
	index = index or 2
	local msg = string.format("[%s]:",string.sub(info.short_src,index + 1))
	-- local result = ...
	-- if type(result) == 'table' then
	-- 	result = utils.dump(args)
	-- end
	skynet.error(msg,...)
end

function log_dump(args)
	local info = debug.getinfo(2)
	local index = string.find(info.short_src, '/',5)  -- 这里以"/"为例  
	-- print(info.short_src,index)
	index = index or 2
	local msg = string.format("[%s]:",string.sub(info.short_src,index + 1))
	local result = utils.dump(args)
	skynet.error(msg,result)
end

function log_debug(...)
	local msg = formatmsg()
	skynet.send("log", "lua", "debug", SERVICE_NAME,msg,...)
end

function log_info(...)
	local msg = formatmsg()
	skynet.send("log", "lua", "info", SERVICE_NAME,msg,...)
end

function log_warning(...)
	local msg = formatmsg()
	skynet.send("log", "lua", "warning", SERVICE_NAME,msg,...)
end

function log_error(...)
	local msg = formatmsg()
	skynet.send("log", "lua", "error", SERVICE_NAME,msg,...)
end

function log_fatal(...)
	local msg = formatmsg()
	skynet.send("log", "lua", "fatal", SERVICE_NAME,msg,...)
end